﻿#region  文件信息

// ---------------------------------------------------------------------------------------
// <copyright file="SendEmail.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：SendEmail.cs
// 创建时间：2015-03-23 19:44
// 修改时间：2015-03-23 19:38
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

namespace WebCommon
{
    #region

    using System.Net;
    using System.Net.Mail;
    using System.Text;

    #endregion

    /// <summary>
    ///     The send email.
    /// </summary>
    public class SendEmail
    {
        #region Fields

        /// <summary>
        ///     The mail message.
        /// </summary>
        private readonly MailMessage mailMessage;

        /// <summary>
        ///     The password.
        /// </summary>
        private readonly string password; // 发件人密码  

        /// <summary>
        ///     The smtp client.
        /// </summary>
        private SmtpClient smtpClient;

        /// <summary>
        /// Gets or sets the host.
        /// </summary>
        public string Host { get; set; }

        /// <summary>
        /// Gets or sets the port.
        /// </summary>
        public int Port { get; set; }

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// 处审核后类的实例
        /// </summary>
        /// <param name="from">
        /// 发件人地址
        /// </param>
        /// <param name="password">
        /// 发件人密码
        /// </param>
        public SendEmail(string from, string password)
        {
            this.mailMessage = new MailMessage();
           
            this.mailMessage.From = new MailAddress(from);

            this.mailMessage.IsBodyHtml = true;
            this.mailMessage.BodyEncoding = Encoding.UTF8;
            this.mailMessage.Priority = MailPriority.Normal;
            this.Port = 25;
            this.Host = "smtp." + this.mailMessage.From.Host;
            this.password = password;
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to">
        /// 收件人地址
        /// </param>
        public void Send(string to)
        {
            if (this.mailMessage != null)
            { 
                this.mailMessage.To.Add(to);


                this.smtpClient = new SmtpClient();
                this.smtpClient.Credentials = new NetworkCredential(this.mailMessage.From.Address, this.password);

                // 设置发件人身份的票据  
                this.smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                this.smtpClient.Host = "smtp." + this.mailMessage.From.Host;
                this.smtpClient.Port = this.Port;
                this.smtpClient.Send(this.mailMessage);
            }
        }

        /// <summary>
        /// 写入标题内容
        /// </summary>
        /// <param name="title"></param>
        /// <param name="body"></param>
        public void SetBody(string title, string body)
        {
            this.mailMessage.Subject = title;
            this.mailMessage.Body = body;
        }

        #endregion
    }
}